
#include "ConnectionManagers.h"

// [*] nw_rbm::CConnectionManager

nw_rbm::CConnectionManager::~CConnectionManager()
{
	this->myRelease();
}

nw_rbm::CConnectionManager* nw_rbm::CConnectionManager::myClone(
	CConnectionManager* pPrototype )
{
	return (pPrototype==NULL ? NULL :
		pPrototype->myClone());
}

void nw_rbm::CConnectionManager::myRelease(
	CConnectionManager* pInstance )
{
	if (pInstance==NULL) return;
	pInstance->myRelease();
}

int nw_rbm::CConnectionManager::GetAmountConnection(
	const CConnectionManager* qConnectionManager, int UnitHandle )
{
	return (qConnectionManager==NULL ? 0 :
		qConnectionManager->GetAmountConnection(UnitHandle));
}

int nw_rbm::CConnectionManager::GetUnitConnectionList(
	const CConnectionManager* qConnectionManager,
	int UnitHandle, CSimpleTypeArray<int>& vTargetHandle, CSimpleTypeArray<int>& vEdgeHandle )
{
	return (qConnectionManager==NULL ? 0 :
		qConnectionManager->GetUnitConnectionList(UnitHandle, vTargetHandle, vEdgeHandle));
}

bool nw_rbm::CConnectionManager::ClearAllConnections(
	CConnectionManager* pConnectionManager )
{
	return (pConnectionManager==NULL ? 0 :
		pConnectionManager->ClearAllConnections());
}

bool nw_rbm::CConnectionManager::ClearUnitConnections(
	CConnectionManager* pConnectionManager,
	int UnitHandle )
{
	return (pConnectionManager==NULL ? 0 :
		pConnectionManager->ClearUnitConnections(UnitHandle));
}

bool nw_rbm::CConnectionManager::NewConnection(
	CConnectionManager* pConnectionManager,
	int UnitHandle, int TargetHandle, int EdgeHandle )
{
	return (pConnectionManager==NULL ? 0 :
		pConnectionManager->NewConnection(UnitHandle, TargetHandle, EdgeHandle));
}

// [*] nw_rbm::CUnitConnections

nw_rbm::CUnitConnections::CUnitConnections()
{
	DeepClear();
}

void nw_rbm::CUnitConnections::Clear()
{
	Dimension = 0;
	vTargetHandle.Clear();
	vEdgeHandle.Clear();
}

void nw_rbm::CUnitConnections::DeepClear()
{
	Dimension = 0;
	vTargetHandle.DeepClear();
	vEdgeHandle.DeepClear();
}

void nw_rbm::CUnitConnections::NewConnection( int TargetHandle, int EdgeHandle )
{
	++Dimension;
	vTargetHandle.PushBack(TargetHandle);
	vEdgeHandle.PushBack(EdgeHandle);
}

// [*] nw_rbm::CConnectionManager_Basic

nw_rbm::CConnectionManager_Basic::CConnectionManager_Basic()
{
	ClearAllConnections();
}

nw_rbm::CConnectionManager* nw_rbm::CConnectionManager_Basic::myClone()
{
	return new CConnectionManager_Basic(*this);
}

void nw_rbm::CConnectionManager_Basic::myRelease()
{
	UnitConnectionDictionary.clear();
}

int nw_rbm::CConnectionManager_Basic::GetAmountConnection( int UnitHandle ) const
{
	CUnitConnectionDictionary::const_iterator itr;
	itr = UnitConnectionDictionary.find(UnitHandle);
	if (itr == UnitConnectionDictionary.end())
		return 0;
	return itr->second.Dimension;
}

int nw_rbm::CConnectionManager_Basic::GetUnitConnectionList(
	int UnitHandle,
	CSimpleTypeArray<int>& vTargetHandle,
	CSimpleTypeArray<int>& vEdgeHandle ) const
{
	CUnitConnectionDictionary::const_iterator itr;
	itr = UnitConnectionDictionary.find(UnitHandle);
	if (itr == UnitConnectionDictionary.end())
	{
		vTargetHandle.Clear();
		vEdgeHandle.Clear();
		return 0;
	}
	const CUnitConnections& tUC = itr->second;
	vTargetHandle = tUC.vTargetHandle;
	vEdgeHandle = tUC.vEdgeHandle;
	return tUC.Dimension;
}

bool nw_rbm::CConnectionManager_Basic::ClearAllConnections()
{
	UnitConnectionDictionary.clear();
	return true;
}

bool nw_rbm::CConnectionManager_Basic::ClearUnitConnections( int UnitHandle )
{
	UnitConnectionDictionary.erase(UnitHandle);
	return true;
}

bool nw_rbm::CConnectionManager_Basic::NewConnection( int UnitHandle, int TargetHandle, int EdgeHandle )
{
	CUnitConnectionDictionary::iterator itr;
	itr = UnitConnectionDictionary.find(UnitHandle);
	if (itr == UnitConnectionDictionary.end())
	{
		CUnitConnections tUC;
		UnitConnectionDictionary[UnitHandle] = tUC;
		itr = UnitConnectionDictionary.find(UnitHandle);
	}
	CUnitConnections& tUC = itr->second;
	tUC.vTargetHandle.PushBack(TargetHandle);
	tUC.vEdgeHandle.PushBack(EdgeHandle);
	return true;
}

